import java.lang.reflect.Array;
import java.util.Arrays;

public class Test2 {
    public static void main(String[] args) {
//        Generic2 generic2 = new Generic2(String, 10);
        //
        Generic2 generic2 = new Generic2(String.class, 10);
        //String.class表示String类类型
        generic2.setTs(0,"Matty");
        System.out.println(generic2);
    }
}

class Generic2<T> {
    public T[] ts;

    public Generic2() {

    }

    @Override
    public String toString() {
        return "Generic2{" +
                "ts=" + Arrays.toString(ts) +
                '}';
    }

    public Generic2(Class<T> tClass, int i) {
//        this.ts = (T[]) new tClass[i];
        //语法错误 tClass不能被识别
        this.ts = (T[]) Array.newInstance(tClass, i);
        //使用Array的方法帮助创建数组
    }

    public T getTs(int i) {
        return this.ts[i];
    }

    public void setTs(int i, T t) {
        this.ts[i] = t;
    }
}